using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using RabbitsWare.Business.TwitterApollo.BusinessEntities;
using System.Data;

namespace RabbitsWare.Business.TwitterApollo.BusinessLogic
{
    public class CompanyManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="?"></param>
        /// <returns>The company id</returns>
        public static int RegisterCompany(string name, int credits, string username, string passwordhash, string email, bool isConfirmed)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloRegisterCompany]");
            database.AddInParameter(storedProcCommand, "@Name", DbType.String, name);
            database.AddInParameter(storedProcCommand, "@Credits", DbType.Int32, credits);
            database.AddInParameter(storedProcCommand, "@UserName", DbType.String, username);
            database.AddInParameter(storedProcCommand, "@PasswordHash", DbType.String, passwordhash);
            database.AddInParameter(storedProcCommand, "@Email", DbType.String, email);
            database.AddInParameter(storedProcCommand, "@Confirmed", DbType.Boolean, isConfirmed);
            
            int companyId = -1;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    companyId = reader.GetInt32(0);
                }
            }

            return companyId;
        }

        public static bool IsUserNameAvaiable(string username)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloIsUsernameAvaiable]");
            database.AddInParameter(storedProcCommand, "@userName", DbType.String, username);
            
            int ret = -1;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    ret = reader.GetInt32(0);
                }
            }

            return ret == 0;
        }

        public static bool DoLogin(string username, string passwordhash)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloLogin]");
            database.AddInParameter(storedProcCommand, "@userName", DbType.String, username);
            database.AddInParameter(storedProcCommand, "@passwordHash", DbType.String, passwordhash);

            int ret = -1;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    ret = reader.GetInt32(0);
                }
            }

            return ret == 1;
        }

        public static Company CreateCompany(string companyName)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloGetCompany]");
            database.AddInParameter(storedProcCommand, "@companyName", DbType.String, companyName);

            Company company = null;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    company = new Company(reader.GetInt32(0), reader.GetString(1), reader.GetInt32(2));
                }
            }

            return company;
        }

        private static Database GetDatabase()
        {
            return DatabaseFactory.CreateDatabase("RabbitsWare");
        }
    }
}
